<?php


namespace app\shop\model;


use app\BaseModel;

class AdminParameterTemplate extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_goods_parameter_template';
    // 自动写入时间戳
    protected $autoWriteTimestamp = true;

    /**
     * @param $where
     * @param $page
     * @功能说明:模板列表
     * @author lxj
     * @DataTime: 2020/5/18 15:01
     */
    public function getList($where,$page){
        $list = $this->where($where)->order('create_time desc')->paginate($page);
        return $list;
    }

    /**
     * @param $where
     * @功能说明:模板下拉选着数据
     * @author lxj
     * @DataTime: 2020/5/19 11:26
     */
    public function adminTemplateList($where){
        $list = $this->where($where)->field('id,name')->select()->toArray();
        return $list;
    }

    /**
     * @param $input
     * @功能说明:返回数据
     * @author lxj
     * @DataTime: 2020/5/18 17:32
     */
    public function returnData($input){
        $data['name']       = $input['name'];
        return $data;
    }

    /**
     * @param $data
     * @功能说明:商品参数模板添加
     * @author lxj
     * @DataTime: 2020/5/18 17:32
     */
    public function templateAdd($data){
        $data['status'] = 1;
        $data['create_time'] = time();
        $res = $this->insertGetId($data);
        return $res;
    }

    /**
     * @param $where
     * @param $goods_id
     * @功能说明:商品参数模板回写数据
     * @author lxj
     * @DataTime: 2020/5/18 17:46
     */
    public function templateInfo($where,$goods_id){
        $data = $this->where($where)->find();
        if(empty($data)){
            return $data;
        }
        $templateInfoModel = new AdminParameterTemplateInfo();
        $where2 = ['a.template_id'=>$data['id'],'a.status'=>1];
        $data['info'] = $templateInfoModel->getInfo($where2,$goods_id);
        return $data;
    }

    /**
     * @param $data
     * @param $where
     * @功能说明:商品参数模板修改
     * @author lxj
     * @DataTime: 2020/5/19 9:49
     */
    public function templateUpdate($where,$data){
        $data['update_time'] = time();
        $res = $this->where($where)->update($data);
        return $res;
    }
    public function isDel($template_id,$parameter_id){
        //判断该模板是否被使用
        $goods_ids = AdminGoods::where(['status'=>1,'template_id'=>$template_id,'is_parameter'=>1])->column('id');
        if(empty($goods_ids)){
            return true;
        }else{
            //判断该参数是否被商品使用
            $parameter_use = AdminGoodsParameterValue::where('goods_id','in',$goods_ids)->where(['status'=>1,'parameter_id'=>$parameter_id])->column('id');
            if(empty($parameter_use)){
                return true;
            }else{
                return false;
            }
        }
    }
}